<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{currentCity}}</div>
            <!-- <div class="button">{{this.$store.state.city}}</div> -->
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" 
          v-for="(item) of hot" 
          :key="item.id" 
          @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" 
        v-for="(item,key) of citiesList" 
        :key="key" 
        :ref="key">
        <div class="title border-topbottom" >{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" 
          v-for="innerItem of item" 
          :key="innerItem.id" 
          @click="handleCityClick(innerItem.name)">
              {{innerItem.name}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Bscroll from "better-scroll"
import {mapState,mapMutations} from 'vuex'
export default {
  name: "CityList",
  props:["hot","citiesList","letter"],
  computed:{
    ...mapState({
      currentCity:'city'
    })
    
  },
  mounted () {
      this.scroll = new Bscroll(this.$refs.wrapper,{ mouseWheel: true, click: true, tap: true })
  },
  watch:{
    //  监听letter变化
    letter () {
        // better-scroll提供一个借口，当this.letter不空的时候this.scroll.scrollToElement()就会滚动到对应的DOM节点
        if(this.letter){
            // this.$refs[key]就会选取到对应的DOM 也就是a-z的div.area
            // this.$refs[this.letter]会得到一个长度为1的数组
            const ele=this.$refs[this.letter][0]
            this.scroll.scrollToElement(ele)
        }
    }
  },
  methods:{
    handleCityClick(city){
      // this.$store.commit("changeCity",city)
      this.changeCity(city)
      this.$router.push('/')
    }, 
    // 有个mutation叫changecity,mutation映射到组件中的一个叫做changeCity的方法里面
    // 如果要调用mutation，就不用this.$store.commit("changeCity",city)了
    ...mapMutations(["changeCity"])
  }
};
</script>
<style scoped lang="stylus" >
@import '~styles/varibles.styl';

.border-topbottom {
  &:before {
    border-color: #777;
  }

  &:after {
    border-color: #777;
  }
}

.border-bottom {
  &:before {
    border-color: #777;
  }
}

.list {
  overflow: hidden;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 1.78rem;

  .title {
    line-height: 0.44rem;
    background: #eee;
    color: #666;
    text-indent: 0.1rem;
  }

  .button-list {
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    overflow: hidden;

    .button-wrapper {
      float: left;
      width: 33.33%;

      .button {
        margin: 0.1rem;
        padding: 0.1rem 0;
        text-align: center;
        border: 0.01rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }

  .item-list, .item {
    line-height: 0.76rem;
    padding-left: 0.2rem;
  }
}
</style>
